/*
* 新人专区商品列表
* @Author: Li Yujie
* @Date: 2020-11-23
*/
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    新人专区
                </div>
            </div>
            <!-- /header -->
            <div class="btn-view">
                <div>
                    <el-button
                        type="primary"
                        size="medium"
                        @click="handleGoodsEdit()"
                    >
                        添加新人专区商品
                    </el-button>
                    <el-button
                        type="primary"
                        size="medium"
                        @click="$refs.goodsSelect.show(2,requestUrl)"
                    >
                        批量添加商品
                    </el-button>
                </div>
                <div>
                    <el-button
                        type="primary"
                        size="medium"
                        style="margin-left: 32px"
                        @click="handleSetting()"
                    >
                        专区配置
                    </el-button>
                </div>
            </div>
            <!-- 筛选-->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">商品名称：</p>
                    <el-input
                        placeholder="请输入商品名称"
                        v-model="filterData.keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="small"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">上架状态：</p>
                    <el-select
                        v-model="filterData.welfare_status"
                        clearable
                        filterable
                        placeholder="全部"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"/>
                    </el-select>
                </div>
                <el-button
                    type="primary"
                    size="small"
                    style="margin-left: 32px"
                    @click="getTableData(1)"
                >
                    查询
                </el-button>

            </div>
            <!--筛选-->

            <!-- table -->
            <el-table
                :data="tableData"
                height="100%"
                style="width: 100%;"
                ref="tempLateName"
                class="data-table"
                @sort-change="handleSortChanged"
            >
                <el-table-column
                    type="index"
                    label="序号"
                    width="80"
                    align="center"
                />
                <el-table-column
                    prop="goods_no"
                    label="商品编号"
                    show-overflow-tooltip
                    align="left"
                    width="150"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="welfare_title"
                    label="商品名称"
                    show-overflow-tooltip
                    align="left"
                    width="200"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="goods_title"
                    label="所链自营商品"
                    show-overflow-tooltip
                    align="left"
                    width="200"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            class="text-button"
                            @click="toGoodsList(scope.row.goods_title)"
                        >
                            {{ scope.row.goods_title }}
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="goods_price"
                    label="原售价"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="新人价"
                    show-overflow-tooltip
                    width="130"
                    align="center"
                >
                    <template slot-scope="scope">
                        {{ scope.row.welfare_price }}
                        <el-button
                            type="text"
                            @click="showPriceDialog(scope.row)"
                        >
                            查看
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="welfare_sort"
                    label="排序号"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="上架状态"
                    show-overflow-tooltip
                    align="center"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.welfare_status === 1">已上架</div>
                        <div v-else-if="scope.row.welfare_status === 0">待上架</div>
                        <div v-else>--</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="添加时间"
                    show-overflow-tooltip
                    align="center"
                    width="200"
                >
                    <template slot-scope="scope">
                        <div>{{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="order_count"
                    label="支付订单"
                    show-overflow-tooltip
                    align="center"
                    width="100"
                    sortable="custom"
                />
                <el-table-column
                    prop="user_count"
                    label="购买用户"
                    show-overflow-tooltip
                    align="center"
                />
                <el-table-column
                    label="佣金比例"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.commission_rate }}%
                    </template>
                </el-table-column>
                <el-table-column
                    label="活动设置总库存"
                    align="center"
                    width="120"
                >
                    <template slot-scope="scope">
                        {{ scope.row.total_stock_origin }}
                        <el-button
                            type="text"
                            @click="showPriceDialog(scope.row)"
                        >
                            查看
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    label="活动剩余总库存"
                    align="center"
                    width="120"
                >
                    <template slot-scope="scope">
                        {{ scope.row.total_stock }}
                        <el-button
                            type="text"
                            @click="showPriceDialog(scope.row)"
                        >
                            查看
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    label="是否平台补贴"
                    align="center"
                    width="120"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="showPriceDialog(scope.row)"
                        >
                            查看
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    width="140"
                    align="left"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <div class="option-btn-group">
                            <el-button
                                type="text"
                                @click="handleGoodsEdit(scope.row.welfare_no)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                type="text"
                                @click="handleGoodsOption(scope.row)"
                            >
                                {{ scope.row.welfare_status === 1 ? '下架' : '上架' }}
                            </el-button>
                            <el-button
                                type="text"
                                @click="handleGoodsDelete(scope.row.welfare_no)"
                            >
                                删除
                            </el-button>
                            <el-button
                                type="text"
                                @click="toOrderDetail(scope.row.welfare_no)"
                            >
                                购买详情
                            </el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <!-- /table -->

            <!-- /分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    background
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>
        <!--商品选择弹窗-->
        <goods-select
            ref="goodsSelect"
            @change="handleSelectGoods"
        />
        <!-- 查看价格弹窗 -->
        <el-dialog
            title="查看商品详情"
            :visible.sync="productVisible"
            width="1050px"
            :close-on-click-modal="false"
        >
            <el-table
                :data="productList"
                height="100%"
                style="width: 100%;max-height: 450px"
                class="data-table"
            >
                <el-table-column
                    prop="spec_list"
                    label="规格名称"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    width="150"
                >
                    <template slot-scope="scope">
                        {{ scope.row.spec_list | filterSpec }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="product_price"
                    label="原售价"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="welfare_price"
                    label="新人价(元)"
                    show-overflow-tooltip
                    align="center"
                    width="120"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="welfare_stock_origin"
                    label="活动设置库存"
                    show-overflow-tooltip
                    align="center"
                    width="120"
                />
                <el-table-column
                    label="活动剩余库存"
                    show-overflow-tooltip
                    align="center"
                    width="120"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.welfare_stock === -1 ? scope.row.product_stock : scope.row.welfare_stock }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="是否平台补贴"
                    show-overflow-tooltip
                    align="center"
                    width="120"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.shop_subsidy_enable === 1">平台补贴</div>
                        <div v-if="scope.row.shop_subsidy_enable === 0">商家全部补贴</div>
                        <div v-if="scope.row.shop_subsidy_enable === 2">商家部分补贴</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="shop_subsidy_money_by_shop"
                    label="商家补贴金额(元)"
                    align="center"
                    width="130"
                >
                    <template slot-scope="scope">
                        {{ calcPlatformMoney(scope.row).shop_subsidy_money_by_shop }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="shop_subsidy_money_by_shop"
                    label="平台补贴金额(元)"
                    align="center"
                    width="130"
                >
                    <template slot-scope="scope">
                        {{ calcPlatformMoney(scope.row).shop_subsidy_money_by_platform }}
                    </template>
                </el-table-column>
            </el-table>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    @click="productVisible = false"
                    size="medium"
                    type="primary"
                >
                    我知道了
                </el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {back} from '@/mixin/back';
import GoodsSelect from '@/components/goods/goods-select';

export default {
    name: "NewPeopleList",
    data() {
        return {
            filterData: {
                keyword: '',
                welfare_status: ''
            },
            tableData: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            statusList: [
                {label: '全部', value: ''},
                {label: '已上架', value: 1},
                {label: '已下架', value: 0}
            ],
            authResult: {},
            productVisible: false,
            productList: [],
            requestUrl: '/student/shop_welfare%5Cget_goods_list'
        };
    },
    mixins: [back],
    components: {
        GoodsSelect,
    },
    filters: {
        filterSpec (val) {
            if(!val) return '';
            let str = '';

            val.forEach(el => {
                str += el.spec_name + ':' + el.spec_value + ';';
            });
            return str;
        }
    },
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '新人专区'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {

        /**
         * 获取表格数据
         * @param page
         */
        getTableData(page) {
            let params = {...this.filterData};

            params.page_id = page || this.page.now_page;
            this.$post('/student/shop_welfare%5Cget_list', params, resp => {
                if (resp.code === 1) {
                    this.tableData = resp.data.list;
                    this.page = resp.data.page;
                } else {
                    this.$notify({
                        title: '失败',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },
        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },
        /**
         * 跳转编辑商品
         * @param no
         */
        handleGoodsEdit(no = '') {

            this.$router.push({
                path: '/student/new-people-edit',
                query: {
                    no
                }
            });
        },

        /**
         * 删除专区商品
         * @param no
         */
        handleGoodsDelete(no) {
            if(!this.authResult.auth_delete) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$confirm('此操作将删除该商品, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    welfare_no: no
                };

                this.$post('/student/shop_welfare%5Cdelete', params, resp => {
                    if (resp.code === 1) {
                        this.$message.success('删除成功');
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '失败',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            });
        },

        /**
         * 查看购买详情
         * @param no
         */
        toOrderDetail(no) {
            let routeData = this.$router.resolve({
                name: 'OrderList',
                query: {
                    from_no: no,
                    type: 8
                }
            });

            window.open(routeData.href, '_blank');
        },

        /**
         * 跳转专区设置页面
         */
        handleSetting() {
            this.$router.push({
                path: '/student/new-people-setting',
            });
        },

        /**
         * 获取批量选择的商品
         * @param val
         */
        handleSelectGoods(val) {
            this.$router.push({
                path: '/student/goods-edit-batch',
                query: { goodsList: JSON.stringify(val) }
            });
        },

        /**
         * 商品上下架
         * @param row
         */
        handleGoodsOption(row) {
            if(!this.authResult.auth_modify) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$confirm(`确认${row.welfare_status === 1 ? '下架' : '上架'}该商品吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    welfare_no: row.welfare_no,
                    welfare_status: row.welfare_status === 1 ? 0 : 1
                };

                this.$post('/student/shop_welfare%5Cmodify', params, resp => {
                    if (resp.code === 1) {
                        this.$message.success('操作成功');
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '失败',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            });
        },

        /**
         * 跳转商品列表
         * @param title
         */
        toGoodsList(title) {
            let routeData = this.$router.resolve({
                name: 'GoodsList',
                query: {
                    goods_title: title,
                    goods_type: 'self'
                }
            });

            window.open(routeData.href, '_blank');
        },

        /**
         * 数据排序
         * @param data
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 'asc',
                descending: 'desc'
            };

            if (data.order) {
                this.filterData.sort_field = data.prop;
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },

        /**
         * 显示详细价格弹窗
         * @param row
         */
        showPriceDialog(row) {
            this.productList = row.product_list;
            this.productVisible = true;
        },

        /**
         * 计算平台与商家补贴金额
         * @param row
         * @returns {{shop_subsidy_money_by_platform: number, shop_subsidy_money_by_shop: string}|{shop_subsidy_money_by_platform: string, shop_subsidy_money_by_shop}|{shop_subsidy_money_by_platform: string, shop_subsidy_money_by_shop: number}}
         */
        calcPlatformMoney(row) {
            let product_price = Number(row.product_price);
            let welfare_price = Number(row.welfare_price);
            let subsidy_money_all = (product_price - welfare_price).toFixed(2);

            // 平台全部补贴
            if(row.shop_subsidy_enable === 1) {
                return {
                    shop_subsidy_money_by_shop: 0,
                    shop_subsidy_money_by_platform: subsidy_money_all
                };
            }
            // 商家全部补贴
            if(row.shop_subsidy_enable === 0) {
                return {
                    shop_subsidy_money_by_shop: subsidy_money_all,
                    shop_subsidy_money_by_platform: 0
                };
            }
            // 商家部分补贴
            if(row.shop_subsidy_enable === 2) {
                let shop_subsidy_money_by_platform = subsidy_money_all - Number(row.shop_subsidy_money_by_shop);

                return {
                    shop_subsidy_money_by_shop: row.shop_subsidy_money_by_shop,
                    shop_subsidy_money_by_platform: shop_subsidy_money_by_platform.toFixed(2)
                };
            }
        }
    }
};
</script>

<style scoped lang="less">
.wrapper {
    width: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
    box-sizing: border-box;
    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;
    }

    /*header*/

    .header {
        height: 68px;
        padding: 24px;
        box-sizing: border-box;
        box-shadow: 0px -1px 0px 0px #F0F2F5;
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-bottom: 1px solid #F0F2F5;

        .header-title {
            color: #333333;
            font-size: 20px;
            height: 20px;
            font-weight: 500;
            position: relative;
            margin: 24px 0 24px 12px;

            &::before {
                content: '';
                position: absolute;
                left: -8px;
                top: -1px;
                width: 3px;
                height: 20px;
                background: #1890FF;
            }
        }
    }
    .btn-view {
        height: 48px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 24px 24px 0;
    }

    /*筛选*/

    .filter-view {
        margin: 24px 0;
        display: flex;
        align-items: center;

        .header-new-box {
            display: flex;
            align-items: center;
            color: #606266;
            white-space: nowrap;
            margin-left: 32px;

            &-title {
                margin-right: 2px;
                white-space: nowrap;
            }
        }
    }

    .status-wrapper {
        display: flex;
        align-items: center;

        .status-dot {
            width: 6px;
            height: 6px;
            border-radius: 50%;
            margin-right: 5px;

            &.success {
                background: #4CD964;
            }

            &.danger {
                background: #FF3030;
            }

            &.warning {
                background: #FFA72D;
            }

            &.normal {
                background: #C2C2C2;
            }
        }
    }

    /* 分页 */

    .pagination-wrapper {
        padding: 10px 32px;

        .el-pagination {
            float: right;
            padding: 0;
            background-color: transparent;
        }
    }

    .text-button {
        width: 180px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        text-align: left;
    }
    // 表格
    .data-table {
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
        // 操作按钮
        .option-btn-group {
            .el-button {
                margin-left: 0;
                margin-right: 10px;
            }
        }
    }

    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    .dialog-footer {
        display: flex;
        justify-content: center;
    }
    /deep/ .el-table__fixed-right {
        right: 6px!important;
    }
}
</style>
